<?php

class My_View_Helper_MyInlineScript extends Zend_View_Helper_HeadScript
{
    /**
     * Registry key for placeholder
     * @var string
     */
    protected $_regKey = 'My_View_Helper_MyInlineScript';

    /**
     * Return InlineScript object
     *
     * Returns InlineScript helper object; optionally, allows specifying a
     * script or script file to include.
     *
     * @param  string $mode Script or file
     * @param  string $spec Script/url
     * @param  string $placement Append, prepend, or set
     * @param  array $attrs Array of script attributes
     * @param  string $type Script type and/or array of script attributes
     * @return My_View_Helper_MyInlineScript
     */
    public function myInlineScript($mode = Zend_View_Helper_HeadScript::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript')
    {
        return $this->headScript($mode, $spec, $placement, $attrs, $type);
        //My_Logger::getInstance()->dump($return);
        return $return;
    }

    /**
     * Start capture action
     *
     * @param  mixed $captureType
     * @param  string $typeOrAttrs
     * @return void
     */
    public function captureStart($captureType = Zend_View_Helper_Placeholder_Container_Abstract::APPEND, $type = 'text/javascript', $attrs = array())
    {
        if ($this->_captureLock) {
            require_once 'Zend/View/Helper/Placeholder/Container/Exception.php';
            $e = new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest headScript captures');
            $e->setView($this->view);
            throw $e;
        }

        $this->_captureLock        = true;
        $this->_captureType        = $captureType;
        $this->_captureScriptType  = $type;
        $this->_captureScriptAttrs = $attrs;
        ob_start();
    }

    /**
     * End capture action and store
     *
     * @return void
     */
    public function captureEnd()
    {
        $content                   = ob_get_clean();
        $type                      = $this->_captureScriptType;
        $attrs                     = $this->_captureScriptAttrs;
        $this->_captureScriptType  = null;
        $this->_captureScriptAttrs = null;
        $this->_captureLock        = false;

        switch ($this->_captureType) {
            case Zend_View_Helper_Placeholder_Container_Abstract::SET:
            case Zend_View_Helper_Placeholder_Container_Abstract::PREPEND:
            case Zend_View_Helper_Placeholder_Container_Abstract::APPEND:
                $action = strtolower($this->_captureType) . 'Script';
                break;
            default:
                $action = 'appendScript';
                break;
        }
        // Retiro las etiquetas de inicio y fin del script <script></script>
        $content = trim($this->stripScriptTags($content));
        //My_Logger::getInstance()->dump($content, 'Contenido sin etiquetas de script:');
        $this->$action($content, $type, $attrs);
    }

    /**
     * @param string $text
     * @return string
     */
    protected function stripScriptTags($text)
    {

        $tags = 'script';
        // replace php and comments tags so they do not get stripped
        $text = preg_replace("@<\?@", "#?#", $text);
        $text = preg_replace("@<!--@", "#!--#", $text);

        // strip tags normally
        $text = strip_tags($text, $tags);

        // return php and comments tags to their origial form
        $text = preg_replace("@#\?#@", "<?", $text);
        $text = preg_replace("@#!--#@", "<!--", $text);

        return $text;
    }
}

